<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../style/manage.css">
    <link rel="stylesheet" href="../bootstrap/bootstrap.min.css">
    <script src="../bootstrap/jquery-3.6.0.js"></script>
    <script src="../bootstrap/bootstrap.min.js"></script>
    <script src="../js/ajax.js"></script>
    <style>
    </style>
</head>

<body>
    <header>
        <h1>宿舍管理系统、、我是dev分支</h1>
        <div class="meg">
            <span>zyp</span>
            <a href="../login.html">退出</a>
        </div>
    </header>
    <div class="left">
        <ul id="nav">
            <li>
                <h2>员工管理</h2>
                <ul class="nav2">
                    <li class="active">查看员工</li>
                    <li>添加新员工</li>
                </ul>
            </li>
            <li>
                <h2>新闻管理</h2>
                <ul class="er nav3">
                    <li>查看新闻</li>
                    <li>添加新闻</li>
                </ul>
            </li>
        </ul>
    </div>
    <div class="right1">
        <div class="chakan_y">
            <div class="search">
                <input type="text" id="ser" placeholder="请输入姓名">
                <button type="button" class="btn btn-secondary" id="serbtn">搜索</button>
            </div>
            <table id="tab">
                <tr>
                    <td>编号</td>
                    <td>姓名</td>
                    <td>入职时间</td>
                    <td>离职时间</td>
                    <td>公司职务</td>
                    <td>职位</td>
                    <td>等级</td>
                    <td>图片</td>
                    <td>操作</td>
                </tr>
            </table>
            <!-- 更新的盒子 -->
            <div class="update_con">
                <div id="upd">
                    <span class="guanbi">关闭</span>
                    <form action="" id="upd_tab">
                        <label for="">编号:</label><input type="text" id="u1"><br>
                        <label for="">姓名:</label><input type="text" id="u2"><br>
                        <label for="">入职时间:</label><input type="text" id="u3"><br>
                        <label for="">离职时间:</label><input type="text" id="u4"><br>
                        <label for="">公司职务:</label><input type="text" id="u5"><br>
                        <label for="">职位:</label><input type="text" id="u6"><br>
                        <label for="">等级:</label><input type="text" id="u7"><br>
                        <label for="">图片:</label><input type="text" id="u8"><br><br>
                        <button type="button" class="btn btn-danger" id="uadd">确认</button>
                        <button type="button" class="btn btn-primary" id="uclear">清空</button>
                    </form>
                </div>
            </div>
            <!-- 搜索的盒子 -->
            <div class="search_con">
                <div class="ser_con">
                    <span class="guanbi_ser">关闭</span>
                    <table id="tab_ser">
                        <tr id="onetr">
                            <td>编号</td>
                            <td>姓名</td>
                            <td>入职时间</td>
                            <td>离职时间</td>
                            <td>公司职务</td>
                            <td>职位</td>
                            <td>等级</td>
                            <td>图片</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <div class="add_y">
            <form action="" id="add_tab">
                <label for="">编号:</label><input type="text" id="i1"><br>
                <label for="">姓名:</label><input type="text" id="i2"><br>
                <label for="">入职时间:</label><input type="text" id="i3"><br>
                <label for="">离职时间:</label><input type="text" id="i4"><br>
                <label for="">公司职务:</label><input type="text" id="i5"><br>
                <label for="">职位:</label><input type="text" id="i6"><br>
                <label for="">等级:</label><input type="text" id="i7"><br>
                <label for="">图片:</label><input type="text" id="i8"><br><br>
                <button type="button" class="btn btn-danger" id="add">确认</button>
                <button type="button" class="btn btn-primary" id="clear">清空</button>
            </form>
            <div class="alert alert-success" id="add_success">
                <strong>添加成功!</strong>
            </div>
        </div>
    </div>
    <div class="right2">
        <div class="chakan_x">
            <div class="top"><span>标题</span><span>时间</span><span>操作</span></div>
            <ul id="xw_con"></ul>
            <!-- 修改新闻的盒子 -->
            <div class="update_con_xw">
                <div id="upd_xw">
                    <span class="guanbi2">关闭</span>
                    <label>新闻标题：</label><input type="text" id="upd_title"><br>
                    <label>新闻内容：</label><textarea id="upd_text" cols="30" rows="10"></textarea><br>
                    <button type="button" class="btn btn-primary" id="upd_xinwen">确认修改</button>
                    <button type="button" class="btn btn-danger" id="clear_text">清空输入</button>
                </div>
            </div>
            <div class="search_xw"><label>请输入新闻标题：</label><input type="text"> <button type="button"
                    class="btn btn-danger" id="search_news">搜索</button>
            </div>
            <!-- 搜索的盒子 -->
            <div class="search_news_con">
                <div id="search_news_con">
                    <span class="guanbi3">关闭</span>
                    <ul id="ulNews"></ul>
                </div>
            </div>

        </div>
        <div class="add_x">
            <div class="addNewsCon">
                <label for="">请输入新闻标题：</label><input type="text" name="" id="title"><br><br>
                <label for="">请输入新闻内容：</label>
                <textarea name="" id="x_text" cols="30" rows="10"></textarea><br>
                <button type="button" class="btn btn-primary" id="add_xinwen">确认添加</button>
                <button type="button" class="btn btn-danger" id="clear_xinwen">清空输入</button>
            </div>
        </div>
    </div>
</body>
<script>
    if (document.cookie != 'ok') {
        location.href = '../login.html';
    }

    $(function () {
        $('h2').click(function () {
            $(this).next().slideDown().parent().siblings().children().eq(1).slideUp();
        });
        $('.nav2').on('click', 'li', function () {
            $('.active').removeClass('active');
            $(this).addClass("active");
            $('.right2').children().hide()
            $('.right1').children().eq($(this).index()).show().siblings().hide();
        });
        $('.nav3').on('click', 'li', function () {
            $('.active').removeClass('active');
            $(this).addClass("active");
            $('.right1').children().hide()
            $('.right2').children().eq($(this).index()).show().siblings().hide();
        });


    })

    var coachData = {};//自己的数据

    myAjax("post", "http://www.qhdlink-student.top/student/coach.php", "username=zyp&userpwd=123456&userclass=70&type=4", text)
    function text(data) {
        coachData = data;//coachData 存取数据
    }
    // if (localStorage.getItem("ldata")) {
    //     coachData = JSON.parse(localStorage.getItem("ldata"));
    // }

    // coachData = JSON.parse(localStorage.getItem("ldata"));
    // console.log(coachData)
    function chakanY(c) {
        for (var key in c) {
            var tr = document.createElement("tr");
            for (var ckey in c[key]) {
                var td = document.createElement("td");
                if (ckey === "path_coach") {
                    td.innerHTML = "<img src='http://www.qhdlink-student.top/" + c[key][ckey] + "'>"
                }
                else {
                    td.innerHTML = c[key][ckey];
                }
                tr.appendChild(td);
            }
            tr.innerHTML += "<td><a class='uptate'>修改</a> &nbsp;&nbsp;<a class='del'>删除</a></td>"
            document.getElementById("tab").appendChild(tr);
        }
    }//生成员工表格
    // chakanY();
    // var n;
    setTimeout(function () {
        coachData["4"] = {
            id_coach: "66",
            name_coach: "王奥",
            dage_coach: "05/10/2022",
            tage_coach: "05/10/2022",
            type_coach: "CEO",
            honor_coach: "老板",
            evaluate_coach: "10",
            path_coach: ""
        }
        coachData["5"] = {
            id_coach: "88",
            name_coach: "腊飞龙",
            dage_coach: "05/10/2022",
            tage_coach: "05/10/2022",
            type_coach: "经理1",
            honor_coach: "老板",
            evaluate_coach: "10",
            path_coach: ""
        }
        coachData["6"] = {
            id_coach: "99",
            name_coach: "张子祺",
            dage_coach: "05/10/2022",
            tage_coach: "05/10/2022",
            type_coach: "经理2",
            honor_coach: "老板",
            evaluate_coach: "10",
            path_coach: ""
        }
        // localStorage.setItem("ldata", JSON.stringify(coachData)); 
        if (localStorage.getItem("ldata") != null) {
            coachData = JSON.parse(localStorage.getItem("ldata"));
        }
        // console.log(coachData)
        chakanY(coachData);
        // n = 0;
        // for (var x in coachData) {
        //     n++;
        // }
    }, 500)//请求数据有延迟


    $(function () {
        document.querySelector('#add').onclick = function () {
            var addCouchData = {};//存取添加的这一列的值
            var id_coach = document.getElementById("i1").value;
            var name_coach = document.getElementById("i2").value;
            var dage_coach = document.getElementById("i3").value;
            var tage_coach = document.getElementById("i4").value;
            var type_coach = document.getElementById("i5").value;
            var honor_coach = document.getElementById("i6").value;
            var evaluate_coach = document.getElementById("i7").value;
            var path_coach = document.getElementById("i8").value;
            document.getElementById('i1').value = '';
            document.getElementById('i2').value = '';
            document.getElementById('i3').value = '';
            document.getElementById('i4').value = '';
            document.getElementById('i5').value = '';
            document.getElementById('i6').value = '';
            document.getElementById('i7').value = '';
            document.getElementById('i8').value = '';
            addCouchData.id_coach = id_coach;
            addCouchData.name_coach = name_coach;
            addCouchData.dage_coach = dage_coach;
            addCouchData.tage_coach = tage_coach;
            addCouchData.type_coach = type_coach;
            addCouchData.honor_coach = honor_coach;
            addCouchData.evaluate_coach = evaluate_coach;
            addCouchData.path_coach = path_coach;
            var uuid = new Date().getTime() + Math.random().toString(36).substr(2);//生成随机id
            coachData[uuid] = addCouchData;
            var tr = document.createElement("tr");
            // console.log(addCouchData)
            for (var i in coachData) {
                if (i == uuid) {
                    for (var y in coachData[i]) {
                        var td = document.createElement("td");
                        td.innerHTML = coachData[i][y];
                        tr.appendChild(td);
                    }
                }
            }
            tr.innerHTML += "<td><a class='uptate'>修改</a> &nbsp;&nbsp;<a class='del'>删除</a></td>"
            document.getElementById("tab").appendChild(tr);
            localStorage.setItem("ldata", JSON.stringify(coachData));

            //提示框 添加成功
            $('#add_success').fadeIn();
            setTimeout(function () {
                $('#add_success').fadeOut()
            }, 1500)
        }



        $('#tab').on('click', 'a', function () {
            if ($(this).hasClass('del')) {
                if (confirm('确认删除吗？')) {
                    $(this).parent().parent().remove();
                    var bianhao = parseInt($(this).parent().parent().children().eq(0).text());//获取当前删除元素的编号
                    coachDataArr = Object.entries(coachData)//将coachData对象转换为数组
                    // console.log(coachDataArr)
                    var arr = coachDataArr.filter(function (value) {
                        return bianhao != value[1].id_coach
                    })//删除后过滤数组
                    coachData = Object.fromEntries(arr);
                    //将arr数组转换为对象
                    // console.log(coachData)
                    localStorage.setItem("ldata", JSON.stringify(coachData));
                }

            }//删除

            if ($(this).hasClass('uptate')) {
                $('.update_con').fadeIn();
                $('#u1').val($(this).parent().parent().children().eq(0).text());
                $('#u2').val($(this).parent().parent().children().eq(1).text());
                $('#u3').val($(this).parent().parent().children().eq(2).text());
                $('#u4').val($(this).parent().parent().children().eq(3).text());
                $('#u5').val($(this).parent().parent().children().eq(4).text());
                $('#u6').val($(this).parent().parent().children().eq(5).text());
                $('#u7').val($(this).parent().parent().children().eq(6).text());
                var bianhao = parseInt($(this).parent().parent().children().eq(0).text());
                // console.log(bianhao)

                $('#uadd').click(function () {
                    coachDataArr = Object.entries(coachData)//将coachData对象转换为数组
                    var arr_update = [];
                    coachDataArr.forEach(function (value) {
                        if (bianhao == value[1].id_coach) {
                            value[1].id_coach = $('#u1').val()
                            value[1].name_coach = $('#u2').val()
                            value[1].dage_coach = $('#u3').val()
                            value[1].tage_coach = $('#u4').val()
                            value[1].type_coach = $('#u5').val()
                            value[1].honor_coach = $('#u6').val()
                            value[1].evaluate_coach = $('#u7').val()
                            value[1].path_coach = $('#u8').val()
                        }
                        arr_update.push(value);
                    })
                    // console.log(arr_update)
                    //更改后变为原对象
                    coachData = Object.fromEntries(arr_update);
                    // console.log(arr_update);
                    localStorage.setItem("ldata", JSON.stringify(coachData));

                    location.href = './manage.html';
                })

            }//修改
        })
        $('.guanbi').click(function () {
            $('.update_con').fadeOut();
        });
        $('#uclear').click(function () {
            $('#u1').val('');
            $('#u2').val('');
            $('#u3').val('');
            $('#u4').val('');
            $('#u5').val('');
            $('#u6').val('');
            $('#u7').val('');
            $('#u8').val('');
        })
        //清空输入
        $('#clear').click(function () {
            $('#i1').val('');
            $('#i2').val('');
            $('#i3').val('');
            $('#i4').val('');
            $('#i5').val('');
            $('#i6').val('');
            $('#i7').val('');
            $('#i8').val('');
        })
        $('.guanbi_ser').click(function () {
            $('.search_con').fadeOut();
            $('#onetr').parent().siblings().remove();
            $("#ser").val('')
        })
        $('#serbtn').click(function () {
            $('.search_con').fadeIn();
            var uname = $('#ser').val();
            coachDataArr = Object.entries(coachData);
            var arr = coachDataArr.filter(function (value) {
                if (value[1].name_coach.indexOf(uname) != -1)
                    return value[1];
            })
            searchCoachData = Object.fromEntries(arr);
            // console.log(searchCoachData);
            for (var x in searchCoachData) {
                var tr = document.createElement('tr');
                for (var y in searchCoachData[x]) {
                    var td = document.createElement('td');
                    // console.log(searchCoachData[x][y])
                    if (y === "path_coach") {
                        td.innerHTML = "<img src='http://www.qhdlink-student.top/" + searchCoachData[x][y] + "'>"
                    }
                    else {
                        td.innerHTML = searchCoachData[x][y];
                    }
                    tr.appendChild(td);
                }
                document.getElementById("tab_ser").appendChild(tr);
            }

        })
    })


    // 新闻
    var newsData = {};
    var con = document.getElementById('xw_con')
    myAjax("post", "http://www.qhdlink-student.top/student/newsa.php", "username=zyp&userpwd=123456&userclass=70&type=4", xinwen)
    function xinwen(data) {
        newsData = data;
    }

    function chankan_xinwen(data) {
        for (var key in data) {
            var li = document.createElement("li");
            li.innerHTML = "<a target='_blank' href='xinwen.html?m=" + data[key].id_news + "' data-index=" + data[key].id_news + ">" + data[key].title_news + "</a><span>" + data[key].time_news + "</span><button class='xw_update'>修改</button><button class='xw_del'>删除</button>";
            xw_con.appendChild(li);
        }
    }
    setTimeout(function () {
        // console.log(newsData)
        if (localStorage.getItem("newsData") != null) {
            newsData = JSON.parse(localStorage.getItem("newsData"));
        }
        chankan_xinwen(newsData)
    }, 500)
    $(function () {
        $('#add_xinwen').click(function () {
            var newData = {};
            var textsData = {};//存储新闻内容的对象
            var textData = {};
            var title = $('#title').val();
            var text = $('#x_text').val();
            var xwid = new Date().getTime() + Math.random().toString(36).substr(2);//生成随机id
            var date = new Date();
            var nowTime = date.toLocaleString();
            newData.id_news = xwid;
            newData.title_news = title;
            newData.time_news = nowTime;

            var n = 0;
            if (localStorage.getItem("textsData") != null) {
                textsData = JSON.parse(localStorage.getItem("textsData"));
            }
            for (var x in textsData) {
                n++;
            }
            textData.id_news = xwid;
            textData.title_news = title;
            textData.time_news = nowTime;
            textData.info_news = text;
            textsData[n] = textData;
            localStorage.setItem("textsData", JSON.stringify(textsData));

            if (localStorage.getItem("newsData") != null) {
                newsData = JSON.parse(localStorage.getItem("newsData"));
            }
            newsData[xwid] = newData;
            // console.log(newsData)
            localStorage.setItem("newsData", JSON.stringify(newsData));
            location.href = './manage.html';
        })

        // 删除新闻
        $('#xw_con').on('click', 'button', function () {
            if ($(this).hasClass('xw_del')) {
                var index = $(this).parent().children().eq(0).attr('data-index');
                // console.log(index)
                if (localStorage.getItem("newsData") != null) {
                    newsData = JSON.parse(localStorage.getItem("newsData"));
                }
                var newsArr = Object.entries(newsData);
                newsDataArr = newsArr.filter(function (value) {
                    return value[1].id_news != index;
                })
                newsData = Object.fromEntries(newsDataArr);
                localStorage.setItem("newsData", JSON.stringify(newsData));
                // console.log(newsData)
                $(this).parent().remove();
            }
            if ($(this).hasClass('xw_update')) {
                $('.update_con_xw').fadeIn();
                var index = $(this).parent().children().eq(0).attr('data-index');
                textsData = JSON.parse(localStorage.getItem("textsData"));
                for (var x in textsData) {
                    if (index == textsData[x].id_news) {
                        $('#upd_title').val(textsData[x].title_news);
                        $('#upd_text').val(textsData[x].info_news);
                    }
                }
                $('#upd_xinwen').click(function () {
                    textsDataArr = Object.entries(textsData)//将textsData对象转换为数组
                    var arr_upd = [];
                    textsDataArr.forEach(function (value) {
                        if (index == value[1].id_news) {
                            value[1].title_news = $('#upd_title').val();
                            value[1].info_news = $('#upd_text').val();
                            var date = new Date();
                            var nowUpdTime = date.toLocaleString();
                            value[1].time_news = nowUpdTime;
                        }
                        arr_upd.push(value);
                    })
                    //更改后变为原对象
                    textsData = Object.fromEntries(arr_upd);
                    localStorage.setItem("textsData", JSON.stringify(textsData));


                    newsDataArr = Object.entries(newsData)//将textsData对象转换为数组
                    var arr_upd1 = [];
                    newsDataArr.forEach(function (value) {
                        if (index == value[1].id_news) {
                            value[1].title_news = $('#upd_title').val();
                            var date = new Date();
                            var nowUpdTime1 = date.toLocaleString();
                            value[1].time_news = nowUpdTime1;
                        }
                        arr_upd1.push(value);
                    })
                    //更改后变为原对象
                    newsData = Object.fromEntries(arr_upd1);
                    localStorage.setItem("newsData", JSON.stringify(newsData));
                    location.href = 'manage.html'
                })

            }
        })
        $('.guanbi2').click(function () {
            $('.update_con_xw').fadeOut();
        })

        $('.guanbi3').click(function () {
            $('.search_news_con').fadeOut();
            $('#search_news').parent().children().eq(1).val("");
            $('#ulNews').empty();
        })
        //搜索新闻
        $('#search_news').click(function () {
            var newsId;
            var newsTitle;
            var t = $(this).parent().children().eq(1).val().trim();
            if (t.length == 0) {
                alert('请输入标题内容');
            }
            else {
                $('.search_news_con').fadeIn();
                newsData = JSON.parse(localStorage.getItem("newsData"));
                newsDataArr = Object.entries(newsData);
                var arr = newsDataArr.filter(function (value) {
                    if (value[1].title_news.indexOf(t) != -1)
                        return value[1];
                })
                searchNewsData = Object.fromEntries(arr);
                // console.log(searchNewsData);

                for (var key in searchNewsData) {
                    var li = document.createElement("li");
                    li.innerHTML = "<a target='_blank' href='xinwen.html?m=" + searchNewsData[key].id_news + "' data-index=" + searchNewsData[key].id_news + ">" + searchNewsData[key].title_news + "</a><span>" + searchNewsData[key].time_news + "</span>";
                    document.getElementById('ulNews').appendChild(li);
                }
            }

        })
    })

</script>

</html>